// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import './assets/iconfont/iconfont.css'
import'mint-ui/lib/style.css'
import './lib/mui/css/mui.css';
import './lib/mui/css/icons-extra.css';
import {Header} from "mint-ui";
import Vuex from "vuex"
Vue.use(Vuex)

import Mint from 'mint-ui';
Vue.use(Mint);
Vue.component(Swipe.name,Swipe);
Vue.component(SwipeItem.name,SwipeItem);
import { Search,Swipe,SwipeItem} from 'mint-ui';
Vue.config.productionTip = false
import VueResource from "vue-resource";
Vue.use(VueResource);

var car =JSON.parse(localStorage.getItem('car')||'[]')
var  store=new Vuex.Store({
  state:{
     car:car
  },
  mutations:{
        addToCar(state,goodsinfo){
        var flag=false;
          state.car.some(item=>{
            if(item.id==goodsinfo.id){
              item.count+= parseInt(goodsinfo.count);
              flag=true;
            
              return true
            }
          })
           if(!flag){
             state.car.push(goodsinfo)
           }
          localStorage.setItem('car',JSON.stringify(state.car))
    },
    
    updataCart(state,goodsinfo){
      state.car.some(item=>{
        if(item.id==goodsinfo.id){
          item.count=parseInt(goodsinfo.count)
          return true
        }
      })
      localStorage.setItem('car',JSON.stringify(state.car));
    },
    removeFormCar(state,id){
      state.car.some((item,i)=>{
       if(item.id==id){
         state.car.splice(i,1)
        return true;
      }
     })
     localStorage.setItem('car',JSON.stringify(state.car));
   },
   updataGoodsSelected(state,info){
     state.car.some(item=>{
       if(item.id==info.id){
         item.selected=info.selected
         return true;
       }
     })
     localStorage.setItem('car',JSON.stringify(state.car));
   }

  },
  

  getters:{
      getAllCount(state){
        var c=0;
        state.car.forEach(item=>{
          c+=item.count
        })
        return c
      },
      getGoodsCount(state){
        var o={}
        state.car.forEach(item=>{
          o[item.id]=item.count;
        })
        return o;
      },
      getGoodsSelect(state){
        var p={}
        state.car.forEach(item=>{
          p[item.id]=item.selected;
        })
        return p;
      },
      getSum(state){
        var m={
          count:0,
          amount:0
        }
        state.car.forEach(item=>{
          if(item.selected){
            m.count+=item.count
            m.amount+=item.price*item.count
          }
        })
        return m
      }
      
  }
})
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
